<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12" v-show="false">
            <a-form-model-item label="单据状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input v-model="model.status" placeholder="单据状态" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="源单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceSerial">
              <a-input v-model="model.sourceSerial" placeholder="请输入源单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="源单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceType">
              <j-dict-select-tag type="list" v-model="model.sourceType" dictCode="hf_purchaseorder_source" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplier">
              <j-dict-select-tag type="list" v-model="model.supplier" dictCode="hf_supplier,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentDate">
              <j-date v-model="model.documentDate" placeholder="请输入单据日期" style="width: 100%"></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="交货日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryTime">
              <j-date v-model="model.deliveryTime" placeholder="请输入交货日期" style="width: 100%" disabled></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="源单id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceId">
              <j-dict-select-tag type="list" v-model="model.sourceId" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customorder">
              <a-input v-model="model.customorder" placeholder="请输入客户订单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="采购方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseType">
              <j-dict-select-tag
                type="list"
                v-model="model.purchaseType"
                dictCode="hf_purchase_type"
                placeholder="请选择采购方式"
                :disabled="checkshow"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="红蓝单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRed">
              <j-dict-select-tag
                type="list"
                v-model="model.isRed"
                dictCode="hf_if_red"
                placeholder="请选择红蓝单"
                :disabled="checkshow"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-search-select-tag
                v-model="model.operator"
                title="制单"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                :disabled="checkshow"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qc">
              <j-search-select-tag
                v-model="model.qc"
                title="品质"
                dict="sys_user,realname,realname"
                placeholder="请选择用户"
                :disabled="checkshow"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="checkshow">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <j-select-user-by-dep v-model="model.checker" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="checkshow">
            <a-form-model-item label="审核日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <a-input v-model="model.checkTime" placeholder="请输入审核日期" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import store from '@/store'
import dayjs from 'dayjs'

export default {
  name: 'HfPurchaseInputHandMadeModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      param1: {
        supplier_id: '',
        from_type: ''
      },
      checkshow: false,
      addshow: false,
      model: {
        purchaseType: '',
        isRed: '2',
        qc: '',
        sourceType: '2'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        supplier: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
        sourceSerial: [{ required: true, message: '源单号不能为空', trigger: 'blur' }],
        documentDate: [{ required: true, message: '单据日期不能为空', trigger: 'blur' }]
      },
      url: {
        add: '/purchase/hfPurchaseInput/add',
        edit: '/purchase/hfPurchaseInput/edit'
      }
    }
  },
  watch: {
    'model.supplier': {
      deep: true,
      immediate: true,
      handler(newValue) {
        this.param1.supplier_id = this.model.supplier
      }
    },
    'model.sourceType': {
      deep: true,
      immediate: true,
      handler(newValue) {
        this.param1.from_type = this.model.sourceType
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    //设置默认制单用户
    this.modelDefault.operator = store.getters.nickname
    this.modelDefault.documentDate = dayjs().format('YYYY-MM-DD HH:mm:ss')
  },
  methods: {
    add() {
      this.edit(this.modelDefault, false)
    },
    edit(record, flag) {
      if (flag === undefined) {
        flag = true
      }
      this.model = Object.assign({}, record)
      this.visible = true
      this.addshow = flag
      this.param1.supplier_id = this.model.supplier
      this.param1.from_type = this.model.sourceType
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    }
  }
}
</script>
